/*
* mixins跨页面共用方法
*/

export default {
  data () {
    return {
      finished: false,
      loading: false,
      isLoading: false,
      isRefresh: false,
      dataList: [],
      page: 1,
      apiType: 'global',
      limit: 10,
      params: {},
      paramsUrl: '',
      isLoaded: true, // 用于骨架屏显示
      apiUrl: ''
    };
  },
  methods: {
    onRefresh () {
      setTimeout(() => {
        this.isRefresh = false;
        this.page = 1;
        this.loading = true;
        this.getList(this.apiUrl, true);
        this.isLoading = false;
      }, 1000);
    },
    // 下拉加载
    onLoad () {
      console.log(this.page);
      // 异步更新数据
      setTimeout(() => {
        this.page++;
        this.isRefresh = false;
        this.getList(this.apiUrl, false);
      }, 1000);
    },
    confirmGoods (data) {
      this.orderInfo = data;
      this.isPopupShow = true;
    },
    getList (type, isRefresh, callback) {
      !this.isRefresh && this.$toast.loading({ duration: 0 });
      this.$API[this.apiType][type]({
        page: this.page,
        limit: this.limit,
        ...this.params
      }, this.paramsUrl).then(res => {
        // 判断是否是购物车的列表，返回的数据增加了一层shopList
        var data = this.apiType === 'cart' ? res.data.shopList : res.data;
        if (res.data && data.length > 0) {
          if (isRefresh) {
            this.dataList = data;
          } else {
            this.dataList = this.dataList.concat(data);
          }
        } else {
          this.finished = true;
          this.dataList = [];
        }
        // 加载状态结束
        this.loading = false;
        if (this.dataList.length === res.count) {
          this.finished = true;
        }
        callback && callback(this.dataList);
        this.isLoaded = false;
        !this.isRefresh && this.$toast.clear();
      });
    }
  }
};
